﻿using SD.Toolkits.Mapper;
using SlamDunk.MES.Domain.Entities.ExecutionContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.ExecutionContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.FoundationContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.PlanContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.ResourceContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.StockContext;
using System;
using System.Collections.Generic;

namespace SlamDunk.MES.AppService.Maps
{
    /// <summary>
    /// 工单相关映射
    /// </summary>
    public static class ExecutionMap
    {
        #region # 工单映射 —— static WorkOrderInfo ToDTO(this WorkOrder workOrder...
        /// <summary>
        /// 工单映射
        /// </summary>
        public static WorkOrderInfo ToDTO(this WorkOrder workOrder, IDictionary<Guid, ExecutionPlanInfo> executionPlanInfos, IDictionary<Guid, FacilityInfo> facilityInfos, IDictionary<Guid, WorkStationInfo> workStationInfos, IDictionary<Guid, TeamInfo> teamInfos, IDictionary<Guid, PersonInfo> personInfos)
        {
            WorkOrderInfo workOrderInfo = workOrder.Map<WorkOrder, WorkOrderInfo>();
            workOrderInfo.ExecutionPlanInfo = executionPlanInfos?[workOrder.ExecutionPlanId];
            workOrderInfo.FacilityInfo = workOrder.FacilityId.HasValue
                ? facilityInfos?[workOrder.FacilityId.Value]
                : null;
            workOrderInfo.WorkStationInfo = workOrder.WorkStationId.HasValue
                ? workStationInfos?[workOrder.WorkStationId.Value]
                : null;
            workOrderInfo.DispatchedTeamInfo = workOrder.DispatchedTeamId.HasValue
                ? teamInfos?[workOrder.DispatchedTeamId.Value]
                : null;
            workOrderInfo.DispatchedPersonInfo = workOrder.DispatchedPersonId.HasValue
                ? personInfos?[workOrder.DispatchedPersonId.Value]
                : null;

            return workOrderInfo;
        }
        #endregion

        #region # 工单人员要求映射 —— static WorkOrderPersonAskInfo ToDTO(this WorkOrderPersonAsk...
        /// <summary>
        /// 工单人员要求映射
        /// </summary>
        public static WorkOrderPersonAskInfo ToDTO(this WorkOrderPersonAsk personAsk, IDictionary<Guid, PersonClassInfo> personClassInfos, IDictionary<Guid, PersonInfo> personInfos, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            WorkOrderPersonAskInfo personAskInfo = personAsk.Map<WorkOrderPersonAsk, WorkOrderPersonAskInfo>();
            personAskInfo.PersonClassInfo = personAsk.PersonClassId.HasValue
                ? personClassInfos?[personAsk.PersonClassId.Value]
                : null;
            personAskInfo.PersonInfo = personAsk.PersonId.HasValue
                ? personInfos?[personAsk.PersonId.Value]
                : null;
            personAskInfo.FacilityInfo = personAsk.FacilityId.HasValue
                ? facilityInfos?[personAsk.FacilityId.Value]
                : null;

            return personAskInfo;
        }
        #endregion

        #region # 工单设备要求映射 —— static WorkOrderEquipmentAskInfo ToDTO(this WorkOrderEquipmentAsk...
        /// <summary>
        /// 工单设备要求映射
        /// </summary>
        public static WorkOrderEquipmentAskInfo ToDTO(this WorkOrderEquipmentAsk equipmentAsk, IDictionary<Guid, EquipmentClassInfo> equipmentClassInfos, IDictionary<Guid, EquipmentInfo> equipmentInfos, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            WorkOrderEquipmentAskInfo equipmentAskInfo = equipmentAsk.Map<WorkOrderEquipmentAsk, WorkOrderEquipmentAskInfo>();
            equipmentAskInfo.EquipmentClassInfo = equipmentAsk.EquipmentClassId.HasValue
                ? equipmentClassInfos?[equipmentAsk.EquipmentClassId.Value]
                : null;
            equipmentAskInfo.EquipmentInfo = equipmentAsk.EquipmentId.HasValue
                ? equipmentInfos?[equipmentAsk.EquipmentId.Value]
                : null;
            equipmentAskInfo.FacilityInfo = equipmentAsk.FacilityId.HasValue
                ? facilityInfos?[equipmentAsk.FacilityId.Value]
                : null;

            return equipmentAskInfo;
        }
        #endregion

        #region # 工单物料要求映射 —— static WorkOrderMaterialAskInfo ToDTO(this WorkOrderMaterialAsk...
        /// <summary>
        /// 工单物料要求映射
        /// </summary>
        public static WorkOrderMaterialAskInfo ToDTO(this WorkOrderMaterialAsk materialAsk, IDictionary<Guid, MaterialInfo> materialInfos, IDictionary<Guid, MaterialLotInfo> materialLotInfos, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            WorkOrderMaterialAskInfo materialAskInfo = materialAsk.Map<WorkOrderMaterialAsk, WorkOrderMaterialAskInfo>();
            materialAskInfo.MaterialInfo = materialInfos?[materialAsk.MaterialId];
            materialAskInfo.MaterialLotInfo = materialAsk.MaterialLotId.HasValue
                ? materialLotInfos?[materialAsk.MaterialLotId.Value]
                : null;
            materialAskInfo.FacilityInfo = materialAsk.FacilityId.HasValue
                ? facilityInfos?[materialAsk.FacilityId.Value]
                : null;

            return materialAskInfo;
        }
        #endregion

        #region # 工单参数要求映射 —— static WorkOrderParameterAskInfo ToDTO(this WorkOrderParameterAsk...
        /// <summary>
        /// 工单参数要求映射
        /// </summary>
        public static WorkOrderParameterAskInfo ToDTO(this WorkOrderParameterAsk parameterAsk, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            WorkOrderParameterAskInfo parameterAskInfo = parameterAsk.Map<WorkOrderParameterAsk, WorkOrderParameterAskInfo>();
            parameterAskInfo.FacilityInfo = parameterAsk.FacilityId.HasValue
                ? facilityInfos?[parameterAsk.FacilityId.Value]
                : null;

            return parameterAskInfo;
        }
        #endregion

        #region # 工单实际人员映射 —— static WorkOrderActualPersonInfo ToDTO(this WorkOrderActualPerson...
        /// <summary>
        /// 工单实际人员映射
        /// </summary>
        public static WorkOrderActualPersonInfo ToDTO(this WorkOrderActualPerson actualPerson, IDictionary<Guid, PersonInfo> personInfos)
        {
            WorkOrderActualPersonInfo actualPersonInfo = actualPerson.Map<WorkOrderActualPerson, WorkOrderActualPersonInfo>();
            actualPersonInfo.PersonInfo = personInfos?[actualPerson.PersonId];

            return actualPersonInfo;
        }
        #endregion

        #region # 工单实际设备映射 —— static WorkOrderActualEquipmentInfo ToDTO(this WorkOrderActualEquipment...
        /// <summary>
        /// 工单实际设备映射
        /// </summary>
        public static WorkOrderActualEquipmentInfo ToDTO(this WorkOrderActualEquipment actualEquipment, IDictionary<Guid, EquipmentInfo> equipmentInfos)
        {
            WorkOrderActualEquipmentInfo actualEquipmentInfo = actualEquipment.Map<WorkOrderActualEquipment, WorkOrderActualEquipmentInfo>();
            actualEquipmentInfo.EquipmentInfo = equipmentInfos?[actualEquipment.EquipmentId];

            return actualEquipmentInfo;
        }
        #endregion

        #region # 工单实际物料映射 —— static WorkOrderActualMaterialInfo ToDTO(this WorkOrderActualMaterial...
        /// <summary>
        /// 工单实际物料映射
        /// </summary>
        public static WorkOrderActualMaterialInfo ToDTO(this WorkOrderActualMaterial actualMaterial, IDictionary<Guid, MaterialInfo> materialInfos, IDictionary<Guid, MaterialLotInfo> materialLotInfos)
        {
            WorkOrderActualMaterialInfo actualMaterialInfo = actualMaterial.Map<WorkOrderActualMaterial, WorkOrderActualMaterialInfo>();
            actualMaterialInfo.MaterialInfo = materialInfos?[actualMaterial.MaterialId];
            actualMaterialInfo.MaterialLotInfo = actualMaterial.MaterialLotId.HasValue
                ? materialLotInfos?[actualMaterial.MaterialLotId.Value]
                : null;

            return actualMaterialInfo;
        }
        #endregion

        #region # 工单实际参数映射 —— static WorkOrderActualParameterInfo ToDTO(this WorkOrderActualParameter)
        /// <summary>
        /// 工单实际参数映射
        /// </summary>
        public static WorkOrderActualParameterInfo ToDTO(this WorkOrderActualParameter actualParameter)
        {
            WorkOrderActualParameterInfo actualParameterInfo = actualParameter.Map<WorkOrderActualParameter, WorkOrderActualParameterInfo>();

            return actualParameterInfo;
        }
        #endregion
    }
}
